ls4=[ 2 for i in range(101) if i%2==0]
print(ls4)
# 1.有N堆硬币，每次只能拿一个或者两个，求最少多少次可以拿完所有硬币（一堆拿完全部，才可拿取下一堆）
# 例如：[10, 8, 5, 3, 27, 99]拿取该六堆硬币需要78次
print('第一题')
ls5=[8,10,27,5,3,99]
num1=0
for i in ls5:
    if i%2==0:
        num1+=i//2
    else:
        num1+=i//2+1
print(f'拿取硬币最少次数{num1}')
# coins = [10, 8, 5, 3, 27, 99]
# count = 0  # 初始化拾取次数
# for coin in coins:
#     if coin % 2 == 0:
#         count += coin / 2
#     else:
#         count += coin // 2 + 1
# print(f'拿取硬币的最少次数是：{int(count)}次')
print('第二题')
# 2.给定一个数字列表，求最大值，最小值，平均值、和
nums = [10, -8, 5, 3, 27, -99]
max_num, min_num = nums[0], nums[0]  # 初始化最大值和最小值
sum_nums = 0
for num in nums:
    sum_nums += num
    if num > max_num:
        max_num = num
    if num < min_num:
        min_num = num
avg_nums = sum_nums / len(nums)
print(f'最大值是：{max_num}，最小值是：{min_num}，和是：{sum_nums}，平均值是：{avg_nums}')

# 3.已知一个unicode值的列表(大小在0~6535之间), 将列表的元素转换成对应的字符串
# print('第3题')
# 例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [97, 98, 99]
result = []
for i in list1:
    result.append(chr(i))
print(result)
# 第二种写法
result1 = [chr(i) for i in list1]
print(result1)
# 4.有两个列表A和B，使用列表C来获取两个列表中公共的元素
# print('第四题')
# A = [1, 'a', 4, 90]
# B = ['a', 8, 'j', 1]
# C=[]
# for i in A:
#     if i in B:
#             C.append(i)
# print(C)
# # [i for i in iter if 条件1]
# C=[i for i in A if  i in B]
# print(C)

# 5.获取列表中出现次数最多的元素
# 例如： — > 打印: 3
nums1 = [1, 2, 3, 1, 4, 2, 1, 3, 7,2, 3]
max_count = 0  # 初始化最大出现次数
max_count_ele_ls = []  # 出现次数最多元素列表
for i in nums1:
    current_num_count = 0  # 计当前i的出现次数
    for j in nums1:
        if j == i:
            current_num_count += 1
    if current_num_count > max_count:
        max_count = current_num_count
        max_count_ele_ls = [i]  # 清空列表，并将该元素加入到列表中
    elif current_num_count == max_count and i not in max_count_ele_ls:
        max_count_ele_ls.append(i)
print(f'该列表中元素出现次数最多的有：{max_count_ele_ls}，它们出现了{max_count}次')

# 6.不重复的使用四个数字：1、8、0、3能组成多少个互不相同的三位数
nums2 = [1, 8, 0, 3]
result_ls = []
for i in nums2:
    if i == 0:
        continue
    for j in nums2:
        if j == i:
            continue
        for z in nums2:
            if z == i or z == j:
                continue
            result_ls.append(''.join(str(i)+str(j)+str(z)))
print(result_ls,len(result_ls))


# 7.给你一个按照非递减顺序排列的整数列表 nums， 和一个目标值 target。请你找出给定目标值在列表中的开始位置和结束位置，如果列表中不存在目标值 target，输出 [-1, -1]
# 例如 nums = [1, 1, 0, 3, 12] target = 1 ，输出[0,1]
print('第七题')



nums9 = [1, 2, 0, 3, 12,1]
ta=1
count_1=0
lis_1=[]
for i in nums9:
    if i==ta:
        lis_1.append(count_1)
    count_1+=1
if lis_1==0:
    lis_1=[-1,-1]
print(lis_1[0],lis_1[-1])
print(nums9.index(0))


# target_index = []
# for i in range(len(nums)):
#     if nums[i] == target:
#         target_index.append(i)
# if not target_index:
#     print([-1,-1])
# elif len(target_index) == 1:
#     print([target_index[0],target_index[0]])
# else:
#     print([target_index[0],target_index[-1]])



# 方法二
# if target not in nums:
#     print([-1,-1])
# elif nums.count(target) == 1:
#     print([nums.index(target),nums.index(target)])
# else:
#     start = nums.index(target)
#     end = len(nums) - nums[::-1].index(target) - 1
#     print([start,end])





# # 8.给你一个字符串及该子串的一个子串，请输出该子串在字符串中出现了几次（考虑重叠情况）
#  例如 string = 'abczzz' substring = 'zz' 输出2
# string = 'abczzz'
# substring = 'zz'
# sub_len = len(substring)  # 子串的长度6
# count = 0  # 初始化计数器
# for i in range(len(string) - sub_len + 1): # 滑动多少次
#     if string[i: sub_len + i] == substring: # 每次怎么滑
#         count += 1
# print(count)
#
# ls4=[ i for i in range(101) if i%2==0]
#     print(lst4)


count=0
string='abczzz'
substring='zz'
sub_len=len(substring)#字串长度
for i in range(len(string)-sub_len+1):
    if string[i:sub_len+i]==substring
        count+=1
print(count)